package com.cp.weixin.util;


import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import java.net.URL;
import java.util.*;

public class HtmlUtil {
	private static Logger log = LogManager.getLogger();
	private static List<String> UA_list=new ArrayList<String>();
	private static List<String> errorMessList=new ArrayList<String>();
	static {
		addAgent();
		initErrorMess();
	}

	public static Document getDocByGet(String url, Map<String, String> header){
		Document doc = null;
		try{
			doc = getCon(url, header).get();
		}catch(Exception ex){
			log.error(ex.getMessage());
		}
		return doc;
	}

	public static Document getDocByGet(String url, int tryTimes, Map<String, String> header){
		Document doc = null;
		for (int i = 1; i <= tryTimes; i++) {
			try{
				return getCon(url, header).get();
			}catch(Exception ex){
				log.error(ex.getMessage());
			}finally{
				if(!htmlError(doc)){
					break;
				}
			}
		}
		return doc;
	}

	private static Connection getCon(String url, Map<String, String> header) {
		try {
			Connection conn = Jsoup.connect(url).header("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3")
					.header("Referer", "http://" + new URL(url).getHost() + "/")
					.header("host", new URL(url).getHost())
					.userAgent(randomUA())
					.ignoreContentType(true)
					.followRedirects(true)
					.ignoreHttpErrors(true);
			if (!header.isEmpty()) {
				Set<String> keys = header.keySet();
				for (String key : keys) {
					log.warn(key +"---" +header.get(key));
					conn.header(key, header.get(key));
				}
			}
			return conn;
		} catch (Exception ex) {
			log.error(ex.getMessage());
			return null;
		}

	}

	private static boolean htmlError(Document doc){
		if(StringUtils.isEmpty(doc.title()))return true;
		for (String error : errorMessList) {
			if(doc.title().contains(error)){
				return true;
			}
		}
		return false;
	}

	private static void initErrorMess(){
		errorMessList.add("出错了");
		errorMessList.add("不存在");
		errorMessList.add("404");
		errorMessList.add("500");
	}


	private static Random random=new Random();

	/**
	 * 随机agent
	 * @return
	 */
	public static String randomUA(){
		return UA_list.get(random.nextInt(UA_list.size()));
	}

	/**
	 * 初始化agent
	 */
	private static void addAgent(){
		UA_list.add("Mozilla/5.0 (Linux; U; Android 2.3.7; en-us; Nexus One Build/FRF91) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1");
		UA_list.add("MQQBrowser/26 Mozilla/5.0 (Linux; U; Android 2.3.7; zh-cn; MB200 Build/GRJ22; CyanogenMod-7) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1");
		UA_list.add("Mozilla/5.0 (Windows NT 6.1; WOW64; rv:7.0a1) Gecko/20110623 Firefox/7.0a1 Fennec/7.0a1");
		UA_list.add("Opera/9.80 (Android 2.3.4; Linux; Opera Mobi/build-1107180945; U; en-GB) Presto/2.8.149 Version/11.10");
		UA_list.add("Mozilla/5.0 (iPhone; U; CPU iPhone OS 4_0 like Mac OS X; en-us) AppleWebKit/532.9 (KHTML, like Gecko) Version/4.0.5 Mobile/8A293 Safari/6531.22.7");
		UA_list.add("Mozilla/5.0 (iPad; U; CPU OS 3_2 like Mac OS X; en-us) AppleWebKit/531.21.10 (KHTML, like Gecko) Version/4.0.4 Mobile/7B334b Safari/531.21.10");
		UA_list.add("Mozilla/5.0 (BlackBerry; U; BlackBerry 9800; en) AppleWebKit/534.1+ (KHTML, like Gecko) Version/6.0.0.337 Mobile Safari/534.1+");
		UA_list.add("Mozilla/5.0 (hp-tablet; Linux; hpwOS/3.0.0; U; en-US) AppleWebKit/534.6 (KHTML, like Gecko) wOSBrowser/233.70 Safari/534.6 TouchPad/1.0");
		UA_list.add("Mozilla/5.0 (SymbianOS/9.4; Series60/5.0 NokiaN97-1/20.0.019; Profile/MIDP-2.1 Configuration/CLDC-1.1) AppleWebKit/525 (KHTML, like Gecko) BrowserNG/7.1.18124");
		UA_list.add("Mozilla/5.0 (compatible; MSIE 9.0; Windows Phone OS 7.5; Trident/5.0; IEMobile/9.0; HTC; Titan)");
		UA_list.add("User-Agent:Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_8; en-us) AppleWebKit/534.50 (KHTML, like Gecko) Version/5.1 Safari/534.50");
		UA_list.add("User-Agent:Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0;");
		UA_list.add("User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:2.0.1) Gecko/20100101 Firefox/4.0.1");
		UA_list.add("User-Agent:Mozilla/5.0 (Windows NT 6.1; rv:2.0.1) Gecko/20100101 Firefox/4.0.1");
		UA_list.add("User-Agent:Opera/9.80 (Macintosh; Intel Mac OS X 10.6.8; U; en) Presto/2.8.131 Version/11.11");
		UA_list.add("User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Maxthon 2.0)");
		UA_list.add("User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Trident/4.0; SE 2.X MetaSr 1.0; SE 2.X MetaSr 1.0; .NET CLR 2.0.50727; SE 2.X MetaSr 1.0)");
		UA_list.add("User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; 360SE)");
		UA_list.add("Mozilla/5.0 (Windows; U; Windows NT 5.2) AppleWebKit/525.13 (KHTML, like Gecko) Chrome/0.2.149.27 Safari/525.13");
		UA_list.add("Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.12) Gecko/20080219 Firefox/2.0.0.12 Navigator/9.0.0.6 ");
		UA_list.add("Mozilla/5.0 (iPhone; U; CPU like Mac OS X) AppleWebKit/420.1 (KHTML, like Gecko) Version/3.0 Mobile/4A93 Safari/419.3");
		UA_list.add("Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36");
	}
}
